$(function() {
	initLoad();
});

function initLoad() {
	var cols = ['ID', '手机号', '设备信息', '配置信息', '任务公式', '创建时间'];
	var models = [{
		name: 'id',
		index: 'id',
		width: 200,
		fixed: true,
		key: true,
		search: false,
		editable: false,
		editoptions: {
			readonly: true
		}
	},
	{
		name: 'mobile',
		index: 'mobile',
		searchoptions: {
			'sopt': ['cn']
		},
		editable: true
	},
	{
		name: 'deviceInfo',
		index: 'deviceInfo',
		search: false,
		editable: true,
		align: 'center'
	},
	{
		name: 'clockType',
		index: 'clockType',
		search: false,
		editable: false,
		align:'left',
		formatter: function(cv, op, rb){
			let html = '';
				html += rb.clockType + ' ' + rb.dateFlag + ' ' + rb.timeFlag;
			return html;
		}
	},
	{
		name: 'cronStr',
		index: 'cronStr',
		search: false,
		editable: false
	},
	{
		name: 'createTime',
		index: 'createTime',
		search: false,
		width: 200,
		formatter: function(cv, op, rb) {
			return num2Date(cv);
		}
	}
	];

    var datas = {
        "sortorder": "ASC",
        "sortname":"createTime"
    };
	loadData("/master/clock", "tabdiv", datas, cols, models);
}

//重载使用
function loadTab(tabDiv, datas) {
	jQuery("#" + tabDiv).jqGrid('setGridParam', {
		datatype: 'json',
		postData: datas
	}).trigger("reloadGrid");
}

//首载使用
function loadData(uriPrefix, tabDiv, datas, cols, models) {
	if (datas.sortorder === undefined) {
		datas.sortorder = 'DESC';
	}
	var hv = $(document).height() - 121;
	$.jgrid.defaults.styleUI = 'Bootstrap';
	$("#" + tabDiv).jqGrid({
		sortorder: datas.sortorder,
		sortname: datas.sortname,
		postData: datas,
		url: uriPrefix + '/list',
		datatype: 'json',
		mtype: 'POST',
		rownumbers: true,
		autowidth: true,
		altRows: true,
		shrinkToFit: true,
		rowNum: 20,
		rowList: [20, 50, 100],
		colNames: cols,
		colModel: models,
		pager: '#' + tabDiv + '_page',
		viewrecords: true,
		hidegrid: false,
		multiselect: true,
		height: hv,
		editurl: uriPrefix + '/crud',
		loadComplete: function(a, b, c) {
			$('#' + tabDiv).jqGrid('setLabel', 'rn', 'No.', {
				'text-align': 'center',
				'vertical-align': 'middle',
				"width": "65"
			});
			$("table[id='tabdiv'] tr").each(function(i, d) {
				$(d).children("td:first").width(52)
			});
		}

	});

	// 底部CURD操作按钮
	$("#" + tabDiv).jqGrid("navGrid", "#" + tabDiv + "_page", {
			edit: false,
			add: false,
			del: true,
			search: true
		},
		{

		},
		{

		},
		{
			closeAfterDel: true,
			reloadAfterSubmit: true,
			beforeShowForm:function(formId){
				//console.log(formId)
			},
			afterComplete:function (a,data,c) {
				var res = a.responseText;
				if("ok" != res){
					alert(res);
				}
			}
		},
		{});

	//显示序号
	$("#" + tabDiv).jqGrid('setLabel', 'rn', 'No', {'text-align': 'left' },'');

	//窗体改变了，改变表格宽度
	$(window).bind('resize',function() {
		window.location.reload();
	});
}
